//分割回文串 II
//测试链接：https://leetcode.cn/problems/palindrome-partitioning-ii/description/
public class MinCut {
    public int minCut(String s) {
        // 1. 预处理
        int n = s.length();
        boolean[][] isPal = new boolean[n][n];
        for (int i = n - 1; i >= 0; i--)
            for (int j = i; j < n; j++)
                if (s.charAt(i) == s.charAt(j))
                    isPal[i][j] = i + 1 < j ? isPal[i + 1][j - 1] : true;

        int[] dp = new int[n];
        for (int i = 0; i < n; i++)
            dp[i] = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            if (isPal[0][i])
                dp[i] = 0;
            else {
                for (int j = 1; j <= i; j++)
                    if (isPal[j][i])
                        dp[i] = Math.min(dp[i], dp[j - 1] + 1);
            }
        }
        return dp[n - 1];
    }
}
